<?php
namespace TwinPeaks\Core;

abstract class CLayout
{
	use TSingleton;
	
	/**
	 * @var array
	 */
	private $templates = [];

	abstract protected function define();
	
	/**
	 * 
	 */
	protected function onCreate()
	{
		$this->define();
	}

	/**
	 * @return void
	 */
	protected function addTemplate($id, $tplObject)
	{
		$tplObject->setLayout($this); // Bind layout
		$this->templates[] = ['id' => $id, 'tpl' => $tplObject];
	}
	
	/**
	 * @return CTemplayte|null
	 */
	function getTemplateById($id)
	{
		foreach ($this->templates as $entry)
			if ($entry['id'] === $id)
				return $entry['tpl'];
		
		return null;
	}
	
	/**
	 * 
	 */
	final function render()
	{
		ob_start();
		
		foreach ($this->templates as $entry)
			$entry['tpl']->render();
		$output = ob_get_clean();

		return $output;
	}
	
	/**
	 * 
	 */
	function renderTemplate($tplId)
	{
		//$
	}
	
	function bindData($data)
	{
		
	}
}